﻿#include <QFile>
#include "CRealTimeDataWidget.h"

CRealTimeDataWidget::CRealTimeDataWidget(CDevBase* pIFDev, QWidget *parent)
	: m_pDev(pIFDev),QWidget(parent)
{
	ui.setupUi(this);
	//setStyleSheetFile();
	//配置定时器
	m_pTimer = new QTimer(this);
	m_pTimer->setTimerType(Qt::TimerType::PreciseTimer);
	connect(m_pTimer, &QTimer::timeout, this, &CRealTimeDataWidget::slotDataRefresh);
}

CRealTimeDataWidget::~CRealTimeDataWidget()
{
	if (m_pTimer != NULL)
	{
		if (m_pTimer->isActive())
		{
			m_pTimer->stop();
		}
		delete m_pTimer;
		m_pTimer = NULL;
	}
}

void CRealTimeDataWidget::setState(bool bConnectState,float fSpeed, float fAccumulatMileage, float fBatteryVol, float fTempDj, float fDriveMoment, 
	float fTemp, float fCo, float fPm25, float fLocation, float fLocationZ, float fBatteryCurrent, float fTempDc, float fLiftMoment,
	float fHum, float fGas, float fPm10)
{
	if (bConnectState)
	{
		ui.labelSpeed->setText(QString::number(fSpeed,'f',1));
		ui.labelAccumulatMileage->setText(QString::number(fAccumulatMileage, 'f', 1));
		ui.labelBatteryVol->setText(QString::number(fBatteryVol, 'f', 1));
		ui.labelTempDj->setText(QString::number(fTempDj, 'f', 1));
		ui.labelDriveMoment->setText(QString::number(fDriveMoment, 'f', 1));
		ui.labelTemp->setText(QString::number(fTemp, 'f', 1));
		ui.labelCo->setText(QString::number(fCo, 'f', 1));
		ui.labelPm25->setText(QString::number(fPm25, 'f', 1));
		ui.labelLocation->setText(QString::number(fLocation, 'f', 1));
		ui.labelLocationZ->setText(QString::number(fLocationZ, 'f', 2));
		ui.labelBatteryCurrent->setText(QString::number(fBatteryCurrent, 'f', 1));
		ui.labelTempDc->setText(QString::number(fTempDc, 'f', 1));
		ui.labelLiftMoment->setText(QString::number(fLiftMoment, 'f', 1));
		ui.labelHum->setText(QString::number(fHum, 'f', 1));
		ui.labelGas->setText(QString::number(fGas, 'f', 1));
		ui.labelPm10->setText(QString::number(fPm10, 'f', 1));
	}
	else
	{
		ui.labelSpeed->setText("#");
		ui.labelAccumulatMileage->setText("#");
		ui.labelBatteryVol->setText("#");
		ui.labelTempDj->setText("#");
		ui.labelDriveMoment->setText("#");
		ui.labelTemp->setText("#");
		ui.labelCo->setText("#");
		ui.labelPm25->setText("#");
		ui.labelLocation->setText("#");
		ui.labelLocationZ->setText("#");
		ui.labelBatteryCurrent->setText("#");
		ui.labelTempDc->setText("#");
		ui.labelLiftMoment->setText("#");
		ui.labelHum->setText("#");
		ui.labelGas->setText("#");
		ui.labelPm10->setText("#");
	}
}

void CRealTimeDataWidget::setStyleSheetFile()
{
	this->setStyleSheet("QGroupBox{border: 1px solid black;margin-top: 2.3ex;border-radius: 10px;background-color: #888888;}"
		"QGroupBox::title{padding: 0 3px; font-size:16px;subcontrol-origin:margin;left: 10px;");
}

void CRealTimeDataWidget::slotDataRefresh()
{
	if (isVisible())
	{
		//刷新机器人实时数据
		CDevDataBase* pDevData = (CDevDataBase*)m_pDev->m_pDevDataBase;
		pDevData->m_pMutex->lock();
		float fSpeed = pDevData->m_sRobCurrSpeed.toFloat();             //当前速度
		float fAccumulatMileage = pDevData->m_sTotalDis.toFloat() / 1000; //累计里程
		float fBatteryVol = pDevData->m_sBatteryVol.toFloat();          //电池电压
		float fTempDj = pDevData->m_sMotorTemp.toFloat();               //电机温度
		float fDriveMoment = pDevData->m_sMotorTorque.toFloat();        //驱动力矩
		float fTemp = pDevData->m_sSensorTemp.toFloat();                //温度
		float fCo = pDevData->m_sSensorCO.toFloat();                    //Co
		float fPm25 = pDevData->m_sSensorPM25.toFloat();                //PM2.5
		float fLocation = pDevData->m_sRobCurrLoca.toFloat() / 1000;    //当前位置
		float fLocationZ = pDevData->m_sRobCurrLocaZ.toFloat() / 1000;         //Z轴位置
		float fBatteryCurrent = pDevData->m_sBatteryCurr.toFloat();     //电池电流
		float fTempDc = pDevData->m_sRobTempInfo.toFloat();             //电池温度
		float fLiftMoment = pDevData->m_sLiftTorque.toFloat();          //升降力矩
		float fHum = pDevData->m_sSensorHum.toFloat();                  //温度
		float fGas = pDevData->m_sSensorGas.toFloat();                  //可燃气体
		float fPm10 = pDevData->m_sSensorPM10.toFloat();                //PM10
		pDevData->m_pMutex->unlock();
		setState(pDevData->m_bPlcConnectState, fSpeed, fAccumulatMileage, fBatteryVol, fTempDj, fDriveMoment, fTemp, fCo, fPm25, fLocation, fLocationZ,
			fBatteryCurrent, fTempDc, fLiftMoment, fHum, fGas, fPm10);
	}
}
